package com.itheima.reggie.service.impl;

import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.mapper.ShoppingCartMapper;
import com.itheima.reggie.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ShoppingCartServiceImpl implements ShoppingCartService {
    @Autowired(required = false)
    private ShoppingCartMapper shoppingCartMapper;

    @Override
    public ShoppingCart add(ShoppingCart shoppingCart) {
        //根据用户的id与dish_id或者setmeal_id查看购物车是否存在该购物项
        ShoppingCart dbshoppingCart = shoppingCartMapper.findUidAndDidOrSid(shoppingCart);
        if (dbshoppingCart == null) {
            //如果查询是空,新插入一条数据
            shoppingCart.setNumber(1);
            shoppingCartMapper.save(shoppingCart);
            dbshoppingCart = shoppingCart;
        }else {
            //补全数据
            //不为空，数量+1
            dbshoppingCart.setNumber(dbshoppingCart.getNumber() + 1);
            shoppingCartMapper.update(dbshoppingCart);
        }

        return dbshoppingCart;

    }

    @Override
    public List<ShoppingCart> findByUserId(Long userId) {
        List<ShoppingCart> shoppingCartList = shoppingCartMapper.findByUserId(userId);
        return shoppingCartList;

    }

    @Override
    public void clean(Long userId) {
        shoppingCartMapper.clean(userId);
    }


}
